package com.norns.torque.dialog
import ando.widget.pickerview.adapter.ArrayWheelAdapter
import ando.widget.wheelview.listener.OnItemSelectedListener
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.hi.dhl.binding.viewbind
import com.norns.torque.databinding.DialogWheelBinding
import com.norns.torque.entrty.DeviceConfig


class WheelDialog:CommonDialog() {
    companion object{
        const val Target:Int=0 //目标值

        const val Precision:Int=1 //精度

        const val Unit:Int=2 //单位

        fun newInstance(type:Int): WheelDialog{
            val args = Bundle()
            args.putInt("type",type)
            val fragment = WheelDialog()
            fragment.arguments = args
            return fragment
        }
    }

    private val binding by viewbind<DialogWheelBinding>()

    val mOptionsItems: MutableList<Int> = ArrayList()
    val mPrecisionOptionsItems: MutableList<String> = ArrayList()
    val mOptionsStringItems: MutableList<String> = ArrayList()
    var selectIndex:Int=0

    private var btnClickListener:((value:Int)->Unit)?=null
    fun setClickListener(listener:(name:Int)->Unit){
        btnClickListener=listener
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        screenWidthAspect=1f
        gravity=Gravity.BOTTOM
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val type=arguments?.getInt("type",0)?:0
        mOptionsItems.clear()
        binding.wheelview.setCyclic(false)
        binding.wheelview.setTextSize(22f)
        if (type== Target){
            //binding.wheelview.setItemsVisibleCount(5)
            for (i in 0 until DeviceConfig.range) {
                mOptionsItems.add(i)
            }
            selectIndex=DeviceConfig.targetValue
            binding.wheelview.currentItem=selectIndex
            binding.wheelview.setAdapter(ArrayWheelAdapter<Int>(mOptionsItems))
        }
        if (type== Precision){
            for (i in 1 until 5) {
                mPrecisionOptionsItems.add("${i}%")
            }
            selectIndex=DeviceConfig.precisionValue
            if (selectIndex>mPrecisionOptionsItems.size){
                selectIndex=mPrecisionOptionsItems.size-1
            }
            binding.wheelview.currentItem=selectIndex
            binding.wheelview.setAdapter(ArrayWheelAdapter<String>(mPrecisionOptionsItems))
        }
        if (type== Unit){
            mOptionsStringItems.clear()
            mOptionsStringItems.add("N.m")
            mOptionsStringItems.add("Ibf.ft")
            mOptionsStringItems.add("Ibf.in")
            mOptionsStringItems.add("kgf.cm")
            mOptionsStringItems.add("kgf.m")
            selectIndex=DeviceConfig.unitValue
            binding.wheelview.currentItem=selectIndex
            binding.wheelview.setAdapter(ArrayWheelAdapter<String>(mOptionsStringItems))
        }

        binding.wheelview.setOnItemSelectedListener(object : OnItemSelectedListener {
            override fun onItemSelected(index: Int) {
                selectIndex=index
            }
        })
        binding.btnConfirm.setOnClickListener {
            if (type== Unit){
                btnClickListener?.invoke(selectIndex)
            }else if (type== Precision) {
                btnClickListener?.invoke(selectIndex+1)
            }else{
                btnClickListener?.invoke(mOptionsItems[selectIndex])
            }
            dismiss()
        }
    }
}